package com.pizza.abstrait;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Hashtable;

import javax.faces.context.FacesContext;

import com.pizza.control.CommandeBean;
import com.pizza.control.PersonnaliserBean;
import com.pizza.model.Diametre;
import com.pizza.model.Ingredient;
import com.pizza.model.Pizza;

/**
 * Class AbstractPizza
 *
 */
public class AbstractPizza implements Serializable {

	protected int id;

	protected String nom="";

	protected String description="";

	protected ArrayList<AbstractPizzaDiametre> diametres;

	protected ArrayList<Ingredient> ingredients;
	
	protected String url_image="";
	
	protected AbstractClient clientPizzaPerso;
	
	protected boolean personnalisee;
	
	protected String listeIngredients="";
	
	protected Float prixOnePers;
	protected Float prixTwoPers;
	protected Float prixFourPers;
	
	protected ArrayList entrySet;
	
	public ArrayList getEntrySet(){
		return entrySet;
	}
	
	public void setEntrySet(ArrayList entrySet){
		this.entrySet = entrySet;
	}
	
	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public ArrayList<AbstractPizzaDiametre> getDiametres() {
		return diametres;
	}

	public void setDiametres(ArrayList<AbstractPizzaDiametre> diametres) {
		this.diametres = diametres;
	}

	public boolean addDiametre(Diametre diametre, float prix) {
		return diametres.add(new AbstractPizzaDiametre(diametre, prix));
	}

	public float getPrix(Diametre diametre) {
		for(int i=0; i< diametres.size(); i++) {
			if(diametres.get(i).getDiametre().equals(diametre)) return diametres.get(i).getPrix();
		}
		return 0;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public ArrayList<Ingredient> getIngredients() {
		return ingredients;
	}

	public void setIngredients(ArrayList<Ingredient> ingredients) {
		this.ingredients = ingredients;
	}

	public boolean addIngredient(Ingredient ingredient) {
		return this.ingredients.add(ingredient);
	}

	public boolean removeIngredient(Ingredient ingredient) {
		return this.ingredients.remove(ingredient);
	}

	public String getNom() {
		return nom;
	}

	public void setNom(String nom) {
		this.nom = nom;
	}
	
	public String getListeIngredients() {
		listeIngredients = "";
		for(int i=0; i< getIngredients().size(); i++) {
			if(i != getIngredients().size() - 1) listeIngredients += getIngredients().get(i).getNom() + ", ";
			else listeIngredients += getIngredients().get(i).getNom();
		}
		return listeIngredients;
	}

	public void setListeIngredients(String listeIngredients) {
		this.listeIngredients = listeIngredients;
	}

	public AbstractClient getClientPizzaPerso() {
		return clientPizzaPerso;
	}

	public void setClientPizzaPerso(AbstractClient clientPizzaPerso) {
		this.clientPizzaPerso = clientPizzaPerso;
	}
	
	public boolean isPersonnalisee() {
		return personnalisee;
	}

	public void setPersonnalisee(boolean personnalisee) {
		this.personnalisee = personnalisee;
	}
	
	public String getUrl_image() {
		return url_image;
	}

	public void setUrl_image(String url_image) {
		this.url_image = url_image;
	}
	
	public String toString() {
		// A REDEFINIR SI BESOIN
		return super.toString();
	}

	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if ((obj == null) || (obj.getClass() != this.getClass()))
			return false;
		Pizza p = (Pizza) obj;
		return (id == p.id && nom.equals(p.nom) && description
				.equals(p.description));
	}

	public Float getPrixFourPers() {
		return diametres.get(2).getPrix();
	}

	public void setPrixFourPers(Float prixFourPers) {
		this.prixFourPers = prixFourPers;
	}

	public Float getPrixOnePers() {
		return diametres.get(0).getPrix();
	}

	public void setPrixOnePers(Float prixOnePers) {
		this.prixOnePers = prixOnePers;
	}

	public Float getPrixTwoPers() {
		return diametres.get(1).getPrix();
	}

	public void setPrixTwoPers(Float prixTwoPers) {
		this.prixTwoPers = prixTwoPers;
	}
	
	public void ajouterPizzaOnePersCommande() {
		FacesContext context = FacesContext.getCurrentInstance();
		CommandeBean commande = (CommandeBean) context.getExternalContext()
		.getSessionMap().get("commandeBean");
		
		if(commande != null) {
			commande.ajouterPizzaPanier(this, diametres.get(0).getDiametre());
		}
	}
	
	public void ajouterPizzaTwoPersCommande() {
		FacesContext context = FacesContext.getCurrentInstance();
		CommandeBean commande = (CommandeBean) context.getExternalContext()
		.getSessionMap().get("commandeBean");
		
		if(commande != null) {
			commande.ajouterPizzaPanier(this, diametres.get(1).getDiametre());
		}
	}
	
	public void ajouterPizzaFourPersCommande() {
		FacesContext context = FacesContext.getCurrentInstance();
		CommandeBean commande = (CommandeBean) context.getExternalContext()
		.getSessionMap().get("commandeBean");
		
		if(commande != null) {
			commande.ajouterPizzaPanier(this, diametres.get(2).getDiametre());
		}
	}
	
	public String personnaliserPizza() {
		FacesContext context = FacesContext.getCurrentInstance();
		PersonnaliserBean personnaliser = (PersonnaliserBean) context.getExternalContext()
		.getSessionMap().get("personnaliserBean");
		
		if(personnaliser != null) {
			personnaliser.Personnaliser(this);
		}
		return "personnaliser";
	}
}
